package l.l.w.CodingInterviews;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 请实现一个函数用来判断字符串是否表示数值（包括整数和小数）。
 * 例如，字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。
 * 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
 */
public class 表示数值的字符串 {

    public boolean isNumeric1(char[] str) {
        Pattern compile = Pattern.compile("[+-]?\\d*[\\.\\d+]?[Ee][[+-]\\d+]|d+?");
        Matcher matcher = compile.matcher(new String(str));
        return matcher.matches();
    }
    public boolean isNumeric(String str) {
        Pattern compile = Pattern.compile("[\\+-]?\\d*(\\.\\d+)?([Ee][[([\\+-]?\\d+)?][\\d*]])?");
        Matcher matcher = compile.matcher(str);
        return matcher.matches();
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true){
            String str = scanner.nextLine();
            boolean result = new 表示数值的字符串().isNumeric(str);
            System.out.println(result);
        }
    }
    public boolean isNumeric(char[] str) {
        if(str == null || str.length == 0) return true;
        int start = 0, len = str.length;
        if(str[0] == '+' || str[0] == '-')start++;
        if(start < str.length ){
            char c = str[start++];
            if(isNumber(c)){    //如果是number，则一一遍历，直到不是
                start = readNumber(str, start);
                if(start == str.length) return true;
                c = str[start++];
            }
            if (c == '.'){  //如果遇到 . 则后面必须有数字，一一遍历数字，直到不是
                int temp = readNumber(str,start);
                if(temp == start) return false;
                if(temp == str.length) return true;
                start = temp;
                c = str[start++];
            }
            if(c == 'e' || c == 'E'){//如果遇到 E，e 后面只能是 + - 和数字
                if(start == str.length) return false;
                if(str[start] == '+' || str[start] == '-'){
                    start++;
                }
                int temp = readNumber(str,start);
                if(temp == start) return false;
                if(temp == str.length) return true;
            }
        }
        return false;
    }

    private boolean isNumber(char c) {
        return c - '0' >= 0 && c - '0' <= 9;
    }

    private int readNumber(char[] str, int start) {
        while (start < str.length){
            if(isNumber(str[start])){
                start++;
            }
            else {
                break;
            }
        }
        return start;
    }
}
